package cn.wing;

import cn.wing.event.OpenSharedMenuPacket;
import cn.wing.event.TransactionInfoMapSync;
import cn.wing.gui.TradingMenu;
import net.minecraft.core.registries.Registries;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.inventory.MenuType;
import net.minecraft.world.item.CreativeModeTab;
import net.minecraft.world.item.Item;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.entity.BlockEntityType;
import net.minecraftforge.common.ForgeConfigSpec;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.common.extensions.IForgeMenuType;
import net.minecraftforge.eventbus.api.IEventBus;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.config.ModConfig;
import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext;
import net.minecraftforge.network.NetworkRegistry;
import net.minecraftforge.network.simple.SimpleChannel;
import net.minecraftforge.registries.DeferredRegister;
import net.minecraftforge.registries.ForgeRegistries;
import net.minecraftforge.registries.RegistryObject;

import static cn.wing.PlayTradingMain.MOD_ID;

@Mod(MOD_ID)
public class PlayTradingMain {

    public static final String MOD_ID = "player_trading_dmzlwkssb";

    public PlayTradingMain(FMLJavaModLoadingContext context) {
        IEventBus modEventBus = context.getModEventBus();
        BLOCKS.register(modEventBus);
        ITEMS.register(modEventBus);
        CREATIVE_MODE_TABS.register(modEventBus);
        MENU_TYPES.register(modEventBus);
        BLOCK_ENTITY.register(modEventBus);
        MinecraftForge.EVENT_BUS.register(this);
        context.registerConfig(ModConfig.Type.COMMON, SPEC);
    }
    //玩家交易方块
    public static final DeferredRegister<Block> BLOCKS = DeferredRegister.create(ForgeRegistries.BLOCKS, MOD_ID);
    //物品
    public static final DeferredRegister<Item> ITEMS = DeferredRegister.create(ForgeRegistries.ITEMS, MOD_ID);
    //创造模式表格
    public static final DeferredRegister<CreativeModeTab> CREATIVE_MODE_TABS = DeferredRegister.create(Registries.CREATIVE_MODE_TAB, MOD_ID);
    //方块菜单
    public static final DeferredRegister<MenuType<?>> MENU_TYPES = DeferredRegister.create(ForgeRegistries.MENU_TYPES, MOD_ID);
    //方块实体
    public static final DeferredRegister<BlockEntityType<?>> BLOCK_ENTITY = DeferredRegister.create(ForgeRegistries.BLOCK_ENTITY_TYPES, MOD_ID);
    public static final RegistryObject<MenuType<TradingMenu>> TRADING_MENU_TYPE = MENU_TYPES.register("trading_menu_type", () -> IForgeMenuType.create((id, inv, data) -> new TradingMenu(id, inv)));

    private static final String PROTOCOL_VERSION = "1.0";
    private static int id = 0;
    public static final SimpleChannel CHANNEL = NetworkRegistry.newSimpleChannel(
            ResourceLocation.fromNamespaceAndPath(MOD_ID, "main"),
            () -> PROTOCOL_VERSION,
            PROTOCOL_VERSION::equals,
            PROTOCOL_VERSION::equals
    );

    public static final SimpleChannel TRANSACTION_INFO_MAP_CHANNEL = NetworkRegistry.newSimpleChannel(
            ResourceLocation.fromNamespaceAndPath(MOD_ID, "transaction_info_map"),
            () -> PROTOCOL_VERSION, PROTOCOL_VERSION::equals, PROTOCOL_VERSION::equals
    );

    public static final SimpleChannel SUB_CHANNEL = NetworkRegistry.newSimpleChannel(
            ResourceLocation.fromNamespaceAndPath(MOD_ID, "sub_channel"),
            () -> PROTOCOL_VERSION, PROTOCOL_VERSION::equals, PROTOCOL_VERSION::equals
    );

    static {
        CHANNEL.registerMessage(
                id++,
                OpenSharedMenuPacket.class,
                OpenSharedMenuPacket::encode,
                OpenSharedMenuPacket::decode,
                OpenSharedMenuPacket::handle
        );
        TRANSACTION_INFO_MAP_CHANNEL.registerMessage(id++, TransactionInfoMapSync.class,
                TransactionInfoMapSync::encode, TransactionInfoMapSync::decode, TransactionInfoMapSync::handle);
        SUB_CHANNEL.registerMessage(
                id++,
                OpenSharedMenuPacket.class,
                OpenSharedMenuPacket::encode,
                OpenSharedMenuPacket::decode,
                OpenSharedMenuPacket::handle
        );
    }

    private static final ForgeConfigSpec.Builder BUILDER = new ForgeConfigSpec.Builder();
    static final ForgeConfigSpec SPEC = BUILDER.build();

}
